package com.longxi.tianjintianbao.ui.activity.assetscheck;

import android.content.Intent;
import android.net.Uri;
import android.provider.Settings;
import android.text.TextUtils;

import com.longxi.tianjintianbao.AppConst;
import com.longxi.tianjintianbao.R;
import com.longxi.tianjintianbao.greedao.controller.DataInventoryController;
import com.longxi.tianjintianbao.greedao.entity.Inventory;
import com.longxi.tianjintianbao.greedao.entity.InventoryChild;
import com.longxi.tianjintianbao.ui.activity.scan.ScanActivity2;
import com.longxi.tianjintianbao.ui.adapter.multitype.assetscheck.TestViewBinder;
import com.longxi.tianjintianbao.ui.base.BaseActivity;
import com.longxi.tianjintianbao.ui.base.BasePresenter;
import com.longxi.tianjintianbao.utils.MyUtils;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.yanzhenjie.permission.Action;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.Permission;

import java.util.ArrayList;
import java.util.List;

import me.drakeet.multitype.Items;
import me.drakeet.multitype.MultiTypeAdapter;

public class TestSearchActivity extends BaseActivity {

    private MultiTypeAdapter adapter;
    private Items items = new Items();
    Inventory inventory;
    private int pages = 0;
    private int limit = 10;
    private String key;

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_test_search;
    }

    @Override
    public void initView() {
        super.initView();
        setTvLeft("返回");
        setTitle("搜索结果");
        key = getIntent().getStringExtra(AppConst.KEY_EXTRAS);
        initAdapter();
        inventory = DataInventoryController.getPatrolByBillid(getIntent().getStringExtra(AppConst.BILLID));

    }

    public void initAdapter(){
        if(adapter == null){
            adapter = new MultiTypeAdapter();
            /* 注册类型和 View 的对应关系 */
            adapter.register(InventoryChild.class, new TestViewBinder(new TestViewBinder.OnClickListener() {
                @Override
                public void onClick(int position) {
                    InventoryChild inventoryChild = (InventoryChild) items.get(position);
                    Intent intent = new Intent(TestSearchActivity.this, TestActivity2.class);
                    intent.putExtra(AppConst.BILLID, inventoryChild.getId());
                    startActivityForResult(intent, AppConst.IntentRequstCode.ACTIVITY_TEST2);
                }
            }));
            mRvContent.setAdapter(adapter);
            adapter.setItems(items);
        }
    }

    @Override
    public void initData() {
        super.initData();
        refresh();
    }

    @Override
    public void initListener() {
        super.initListener();

        mRefreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                pages++;
                refresh();
            }

            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                pages=0;
                items.clear();
                refresh();
            }
        });

    }

    public void scan() {
        AndPermission.with(this)
        .permission(Permission.CAMERA, Permission.READ_EXTERNAL_STORAGE)
        .onGranted(new Action() {
            @Override
            public void onAction(List<String> permissions) {
                Intent intent = new Intent(TestSearchActivity.this, ScanActivity2.class);
                startActivityForResult(intent, AppConst.IntentRequstCode.REQUEST_CODE_SCAN);
            }
        })
        .onDenied(new Action() {
            @Override
            public void onAction(List<String> permissions) {
                Uri packageURI = Uri.parse("package:" + getPackageName());
                Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, packageURI);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                startActivity(intent);

                MyUtils.showToast("没有权限无法扫描");
            }
        }).start();
    }

    private void refresh(){
        List<InventoryChild> list = new ArrayList<>();
        list = DataInventoryController.findByNameOrNumber(inventory.id, key, pages, limit);
        items.addAll(list);
        if(mRefreshLayout.isRefreshing())
            mRefreshLayout.finishRefresh();
        if(mRefreshLayout.isLoading())
            mRefreshLayout.finishLoadMore();

        adapter.notifyDataSetChanged();
        if(items.size() == 0){
            showNoData();
        }else{
            hideLoading();
        }
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(resultCode != RESULT_OK){
            return;
        }

        switch (requestCode) {
            case AppConst.IntentRequstCode.ACTIVITY_TEST2:
                mRefreshLayout.autoRefresh();
                setResult(RESULT_OK);
                break;
        }
    }

}
